#include <QLabel>
#include <QGridLayout>
#include <QLineEdit>
#include <QTextEdit>
#include <QItemSelectionModel>
#include <QCheckBox>
#include "LanguageModel.h"
#include "SymbolModel.h"
#include "TextEdit.h"

#include "EditLanguageView.h"

EditLanguageView::EditLanguageView( QWidget * parent ) :
    QWidget( parent ),
    currentSymbolIndex( -1 )
{
    QGridLayout * layout = new QGridLayout( this );
    
    valueLabel = new QLabel( tr( "Value :" ) );
    valueEdit = new TextEdit();
    valueLabel->setBuddy( valueEdit );

    descriptionLabel = new QLabel( tr( "description :" ) );
    descriptionTextLabel = new QLabel( "" );
    descriptionTextLabel->setMinimumHeight( 100 );
    descriptionLabel->setBuddy( descriptionTextLabel );
    
    statusLabel = new QLabel( tr( "Status :" ) );
    statusBox = new QComboBox( this );

    //could do a clean enum :
    statusBox->addItem( tr( "Prototype" ), 0 );
    statusBox->addItem( tr( "To validate" ), 1 );
    statusBox->addItem( tr( "Valid" ), 2 );

    statusLabel->setBuddy( statusBox );

    layout->addWidget( valueLabel, 0, 0 );
    layout->addWidget( valueEdit, 0, 1 );

    layout->addWidget( statusLabel, 1, 0 );
    layout->addWidget( statusBox, 1, 1 );

    layout->addWidget( descriptionLabel, 2 , 0 );
    layout->addWidget( descriptionTextLabel, 2 , 1 );

   
    selectionModel = 0;

    setLayout( layout );

    connect( statusBox, SIGNAL( currentIndexChanged(int)), this, SLOT( statusChanged() ) );
    connect( valueEdit, SIGNAL( editingFinished() ), this, SLOT( valueEditingFinished() ) );
}

void EditLanguageView::setCurrent( const QModelIndex & current )
{
    currentRow = languageModel->getRowIndex( symbolModel->data( current.sibling( current.row(), 1 ) ).toInt() );
    
    //currentSymbolIndex = symbolModel->data( current.sibling( current.row(), 1 ) ).toInt();
    //index = languageModel->data( current.sibling( currentRow, 2 ) ).toInt();

    statusBox->blockSignals( true );
    statusBox->setCurrentIndex( languageModel->data( languageModel->index( currentRow, 1 ) ).toInt() );
    statusBox->blockSignals( false );
    
    valueEdit->blockSignals( true );
    valueEdit->setText( languageModel->data( languageModel->index( currentRow, 0 ) ).toString() );
    valueEdit->blockSignals( false );

    descriptionTextLabel->blockSignals(true);    
    descriptionTextLabel->setText( symbolModel->data( current.sibling( current.row(), 3 ) ).toString() );
    descriptionTextLabel->blockSignals(false); 

    currentIndex = current;
}

void EditLanguageView::valueEditingFinished()
{
    languageModel->setData( languageModel->index( currentRow, 0 ), valueEdit->toPlainText() );
}

void EditLanguageView::statusChanged()
{
    languageModel->setData( languageModel->index( currentRow, 1 ), statusBox->currentIndex() );
}
